
package com.fyzk.net;

import android.util.TypedValue;

import com.fyzk.activity.app.App;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.Inflater;

import http.DataUtil.text.TextUtil;


/**
 * 共用方法类
 */

public class PublicMethod {

    /**
     * 格式化时间
     *
     * @param longTime
     * @return
     */
    public static String formatDate(long longTime) {
        SimpleDateFormat format_date = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        return format_date.format(longTime);
    }


    /**
     * 将dip转换成px
     *
     * @param dipValue
     * @return
     */
    public static int dip2pix(float dipValue) {
        final float scale = App.getContext().getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }


    /**
     * 字符串加 * 号
     *
     * @param text
     * @param first 开头显示的 位数
     * @param last  尾部显示的 位数
     * @return
     */
    public static String tv_yinCang(String text, int first, int last) {
        String string = "";
        if (null == text) {
            return "(空)";
        }

        if ((first + last) >= text.length()) {
            return text;
        }

        for (int i = 0; i < text.length(); i++) {
            if (i < first) {
                string += text.charAt(i);
            } else if (i >= first && i < (text.length() - last)) {
                string += "*";
            } else {
                string += text.charAt(i);
            }
        }

        return string;
    }


    /**
     * 读取文件
     *
     * @param inputStream
     * @return
     */
    public static String getStringFromStream(InputStream inputStream) {
        InputStreamReader inputStreamReader = null;
        try {
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        BufferedReader reader = new BufferedReader(inputStreamReader);
        StringBuffer sb = new StringBuffer("");
        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String formatBalance(double num) {
        BigDecimal decimal = new BigDecimal(num);
        return decimal.divide(new BigDecimal(100)).toString();
    }


    public static String formatMoney(double num) {
        return formatStringToTwoPoint(num / 100f);
    }

    public static int formatMoney(int num) {
        return num / 100 ;
    }


    public static String getSenderName(String alias,String userName){
        if(TextUtil.isEmpty(alias)){
            if(PublicMethod.isphonenum(userName)){
                String four_mid =  userName.substring(3,7);
                return  userName.replace(four_mid,"****");
            }
            return  userName;
        }
        return  alias;
    }

    /**
     * 保留两位小数
     *
     * @param num
     * @return
     */
    public static String formatStringToTwoPoint(double num) {
        DecimalFormat df1 = new DecimalFormat("#0.00");
        String result = df1.format(num);
        return result;
    }

    /**
     * gzip
     *
     * @param data
     * @return byte[]
     */
    public static byte[] decompress2(byte[] data) {
        byte[] output = new byte[0];

        Inflater decompresser = new Inflater();
        decompresser.reset();
        decompresser.setInput(data);

        ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
        try {
            byte[] buf = new byte[1024];
            while (!decompresser.finished()) {
                int i = decompresser.inflate(buf);
                o.write(buf, 0, i);
            }
            output = o.toByteArray();
        } catch (Exception e) {
            output = data;
            e.printStackTrace();
        } finally {
            try {
                o.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        decompresser.end();
        return output;
    }

    //检查是否11位电话号码
    public static boolean isphonenum(String phonenum) {
        Pattern p = Pattern.compile("^\\d{11}");
        Matcher m = p.matcher(phonenum);
        return m.matches();
    }

    /**
     * 返回总注数竞彩过关投注计算注数
     *
     * @param betcodes
     * @param select
     * @return
     */
    public static int getAllAmt(List<String> betcodes, int select) {
        // 初始化原始数据
        int[] a = new int[betcodes.size()];
        for (int i = 0; i < a.length; i++) {
            a[i] = i;
        }
        // 接收数据
        int[] b = new int[select];

        List<int[]> list = new ArrayList<int[]>();

        // 进行组合
        combine(a, a.length, select, b, select, list);

        // 返回数据对象
        int resultInt = 0;
        for (int[] result : list) {
            int itemNum = 1;
            for (int p : result) {
                itemNum *= Integer.parseInt(betcodes.get(p));
            }
            resultInt += itemNum;
        }

        return resultInt;
    }

    /**
     * 组合的递归算法
     *
     * @param a    原始数据
     * @param n    原始数据个数
     * @param m    选择数据个数
     * @param b    存放被选择的数据
     * @param M    常量，选择数据个数
     * @param list 存放计算结果
     */
    public static void combine(int a[], int n, int m, int b[], final int M, List<int[]> list) {
        for (int i = n; i >= m; i--) {
            b[m - 1] = i - 1;
            if (m > 1)
                combine(a, i - 1, m - 1, b, M, list);
            else {
                int[] result = new int[M];
                for (int j = M - 1; j >= 0; j--) {
                    result[j] = a[b[j]];
                }
                list.add(result);
            }
        }
    }

    /**
     * 竞彩足球竞彩篮球球队名称字体大小
     * @param teamNameTvDefaultTSize 字体原大小
     * @param name
     * @return
     */
    public static float getTeamNameTextSize(float teamNameTvDefaultTSize, String name){
        float txtSize = teamNameTvDefaultTSize/ TypedValue.COMPLEX_UNIT_SP;
        if(name.length()>7){
            return txtSize*(7f/name.length());
        }
        return  txtSize;
    }

    /**
     * 获取3位的场次编号
     * @param matchNum
     * @return
     */
    public static String getTeamNum( String matchNum){
        if(matchNum.length() <= 3){
            return  matchNum;
        }
        return  matchNum.substring(matchNum.length()-3);
    }



}
